home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 194_01 / translit.c < prev   
Text File  |  1985-11-13  |  2KB  |  95 lines

  1. /* [TRANSLIT.C of JUGPDS Vol.17]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12.     Usage: tr from to 
  13. */
  14.  
  15. /* translit - map characters */
  16.  
  17. #include "stdio.h"
  18. #include "def.h"
  19.  
  20. #define    BDS        1
  21.  
  22. #ifdef    BDS
  23. #include <dio.h>
  24. #define    stderr        STDERR
  25. #define    stdin        STDIN
  26. #endif
  27.  
  28. #define MAXSET 256
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char    *argv[];
  33.  
  34. {
  35.     char    from[MAXSET], to[MAXSET], *temp, *ap;
  36.     int    c, allbut, collap, i, lastto;
  37.     
  38. #ifdef    BDS
  39.     dioinit(&argc,argv);
  40. #endif
  41.     if(argc < 2) {
  42.         fprintf(stderr, ">tr from to.\n");
  43.         exit();
  44.         }
  45.     ap = argv[1];
  46.     if (*ap == NOT) {
  47.         allbut = YES;
  48.         ap++;
  49.         }
  50.     else
  51.         allbut = NO;
  52.     if (makset(ap, 0, from, MAXSET) == NO) {
  53.         fprintf(stderr, "from: too large.\n");
  54.         exit();
  55.         }
  56.     ap = argv[2];
  57.     if (argc == 2)
  58.         to[0] = EOS;
  59.     else if (makset(ap, 0, to, MAXSET) == NO) {
  60.         fprintf(stderr, "to: too large.\n");
  61.         exit();
  62.         }
  63.     lastto = strlen(to);
  64.     collap = ( (strlen(from) > lastto || allbut == YES) ? YES : NO );
  65.     lastto--;
  66.     while(1) {
  67.         i = xindex(from, (c=getchar()), allbut, lastto);
  68.         if (collap == YES && i >= lastto && lastto >= 0) {
  69.             putchar(to[lastto]);
  70.             while((    i= xindex(from, (c=getchar()), allbut, lastto))                 >= lastto);
  71.             }
  72.         if (c == EOF)
  73.             break;
  74.         if (i >= 0 && lastto >= 0)
  75.             putchar(to[i]);
  76.         else if (i == ERROR)
  77.             putchar(c);
  78.         }
  79. #ifdef    BDS
  80.     dioflush();
  81. #endif
  82. }
  83.  
  84. makset(array, k, set, size)
  85. char *array, *set;
  86.  
  87. {
  88.     int    i, j;
  89.  
  90.     i = k;
  91.     j = 0;
  92.     filset(EOS, array, &i, set, &j, size);
  93.     return (addset(EOS, set, &j, size));
  94. }
  95.